// 配置API接口地址
// const baseUrl = 'http://www.kuitao8.com/';



import SHA256 from 'crypto-js/sha256'

// import fetchJsonp from 'fetch-jsonp'
// require('es6-promise').polyfill();

// 自定义判断元素类型JS
function toType(obj) {
  return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}

// 参数过滤函数
function filterNull(o) {
  for (var key in o) {
    if (o[key] === null) {
      delete o[key]
    }
    if (toType(o[key]) === 'string') {
      o[key] = o[key].trim()
    } else if (toType(o[key]) === 'object') {
      o[key] = filterNull(o[key])
    } else if (toType(o[key]) === 'array') {
      o[key] = filterNull(o[key])
    }
  }
  return o
}

// 工具方法
function toParams(obj) {
  var param = ""
  for (const name in obj) {
    if (typeof obj[name] != 'function') {
      param += "&" + name + "=" + encodeURI(obj[name])
    }
  }
  return param.substring(1)
};

function uncompile(code) {
  code = decodeURI(code);
  var c = String.fromCharCode(code.charCodeAt(0) - code.length);
  for (var i = 1; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
  }
  return c;
}

/**
 * 接口处理函数
 */
function apiStream(method, url, params, successCallback, errorCallback) {
  // 过滤参数
  if (params) {
    params = filterNull(params)
  }

  let headers = {};
  headers['Authorization'] = 'Basic aHRtbC1jbGllbnQtMDAxLSNAITpodG1sLWNsaWVudC0wMDEtI0Ah';
  //headers['mode'] = 'no-cors'; //允许跨域

  params.httpparams101 = Math.random().toString(36).substr(2);
  let key = '%C2%81%C3%9C%C3%A1%C3%99%C2%99%C2%A5%C3%B1%C3%B3%C2%9BK%5Eggg%C2%99%C3%8A%C3%8A%C3%8A%C3%97%C2%94KMZoq';
  params.httpparams102 = SHA256(params.httpparams101 + uncompile(key)).toString();

  // params.accessToken = window.localStorage.getItem('qqlgClientAccessToken');
  // params.accessUserId = window.localStorage.getItem('qqlgClientUserId');

  let fetchParams={
    method: method,
    headers: headers
    //mode:'cors'
  };

  if (method === 'GET') {
    url = url + '?' + toParams(params)
  }else if(method === 'POST'){
    fetchParams['body']=toParams(params)
  }

  fetch(url,fetchParams).then((res) => {
    return res.json();
  }).then((res) => {
    successCallback(res)
  }).catch((err) => {
    alert(err);
    console.log(err);
    //errorCallback(err)
  })


  // if (method === 'GET') {
  //   fetch(url + '?' + toParams(params), {
  //     method: method,
  //     headers: headers
  //   }).then((res) => {
  //     return res.json();
  //   }).then((res) => {
  //     successCallback(res)
  //   }).catch((err) => {
  //     errorCallback(err)
  //   })
  // } else if (method === 'POST') {
  //   fetch(url,{
  //     method:method,
  //     headers: headers,
  //     body:toParams(params)
  //   }).then((res) => {
  //     return res.json();
  //   }).then((res) => {
  //     successCallback(res)
  //   }).catch((err) => {
  //     errorCallback(err)
  //   })
  // }
};


let ajax = {
  get: function (url, params, successCallback, errorCallback) {
    apiStream('GET', url, params, successCallback, errorCallback)
  },
  post: function (url, params, successCallback, errorCallback) {
    apiStream('POST', url, params, successCallback, errorCallback)
  }
};

export default ajax;


